// console.log('content_scripts');
//此处后续要加提醒到页面右上角以及到点自动完成一些任务的功能
// 创建None对象并连接到background


// 环境设置
// const env = 'dsl';
const env = 'me';
// const env = 'prod';

const COMMON_HEADERS = { 'Content-Type': 'application/json;charset=utf8', 'x-hasura-admin-secret': '9090990' }

const GOODS_TYPES = {
  话费充值: '话费充值',
  实物商品: '实物商品'
}

// 环境变量
const envs = {
  dsl: {
    CMS_API_HOST: 'http://dev-cms-api.stardream.wang:83',
    ADMIN_HOST: 'http://localhost:8080',
  },
  me: {
    CMS_API_HOST: 'http://dev-cms-api.stardream.wang',
    ADMIN_HOST: 'http://dev-cms-vue.stardream.wang',
  },
  prod: {
    CMS_API_HOST: 'https://cms-api.learnmoney.cn',
    ADMIN_HOST: 'https://admin.learnmoney.cn',
    ALIPAY_PHONE: '15587431876',
    ALIPAY_PASSWORD: '369258',

    // 亿路有我官方旗舰店
    // GOODS_LINK: 'https://yangkeduo.com/mall_page.html?mall_id=897709850&ts=1621561039410&has_decoration=0&msn=6a7x7dipe2tfigd55m254dccey_axbuy&refer_share_id=fab730c45c2a4da3b3048863bf38740a&refer_share_uid=3650926838&refer_share_uin=AKB2QB74NX7RW5FZMRWGTGBHDA_GEXDA&refer_share_channel=message&refer_share_form=card',
    GOODS_MAP_BAK: [{
      LABEL: '100元话费充值',
      // 用于在店铺页点击具体商品
      GOODS_TITLE: '【热评过万】【限时特价】全国移动电信联通话费充值-慢充话费',
      // 洪东通信
      GOODS_LINK: 'https://yangkeduo.com/mall_page.html?mall_id=920799&msn=ta6tnwzgfyvymehwaunvu3j3ia_axbuy&mall_info=%7B%22mall_name%22%3A%22%E6%B4%AA%E4%B8%9C%E9%80%9A%E4%BF%A1%E4%B8%93%E8%90%A5%E5%BA%97%22%7D&goods_id=45096694917&refer_page_name=goods_detail&refer_page_id=10014_1625478681720_kmh5ai877k&refer_page_sn=10014&_x_share_id=14751eb4fd7b4a3ab43aeda13827d769',
      ITEM_IDS: [
        // '1407910766221320193',
        // '1405774955916943362',
        // '1394972615702077442',
        // '1394911902484783106',
        // '1375040672927703042',
        '1387677031977353218',
        '1387677933794656258',
        // 快充
        '1435537011763175425',
        '1435535453180780546'
      ],
      // 当付款金额不在此区间时不付款
      PAY_RANGE: [80, 98],
      type: GOODS_TYPES.话费充值,
      // 洪东通信专营店
      async operate(sp, province) {
        await none.delay(100, 200)
          // 去拼单
          .click('#loacl-group-container .Ay9Qe_QL:last-child').delay(1000, 3000)
          // 参与拼单
          .click('button:contains(参与拼单)').delay(1000, 3000)
          // 选择sku运营商
          .click(`div.tWGpNA2Y:contains(中国${sp})`).delay(1000, 3000).run();
        if (sp === '移动' && province === '广东') {
          none.click(`div.tWGpNA2Y:contains(广东移动)`).delay().run();
        }
        none
          // 点击确定
          .click('._2YJgLay-').delay(500, 3000)
          .run({ errResume: true });
      }
    }],
    GOODS_MAP: [{
      LABEL: '100元话费充值',
      // 用于在店铺页点击具体商品
      GOODS_TITLE: '【热评过万】【限时特价】全国移动电信联通话费充值-慢充话费',
      // 洪东通信
      GOODS_LINK: 'https://yangkeduo.com/mall_page.html?mall_id=920799&msn=ta6tnwzgfyvymehwaunvu3j3ia_axbuy&mall_info=%7B%22mall_name%22%3A%22%E6%B4%AA%E4%B8%9C%E9%80%9A%E4%BF%A1%E4%B8%93%E8%90%A5%E5%BA%97%22%7D&goods_id=45096694917&refer_page_name=goods_detail&refer_page_id=10014_1625478681720_kmh5ai877k&refer_page_sn=10014&_x_share_id=14751eb4fd7b4a3ab43aeda13827d769',
      ITEM_IDS: [
        '1435533452673933313',
        '1435533041934131202',
        '1431536465205125121',
        '1430373674637189121',
        '1415137009895333889',
        '1407910766221320193',
        '1394961202652766210',
        // 快充
        // '1435537011763175425',
        // '1435535453180780546'
      ],
      // 当付款金额不在此区间时不付款
      PAY_RANGE: [80, 98],
      type: GOODS_TYPES.话费充值,
      // 洪东通信专营店
      async operate(sp, province) {
        await none.delay(100, 200)
          // 去拼单
          .click('#loacl-group-container .Ay9Qe_QL:last-child').delay(1000, 3000)
          // 参与拼单
          .click('button:contains(参与拼单)').delay(1000, 3000)
          // 选择sku运营商
          .click(`div.tWGpNA2Y:contains(中国${sp})`).delay(1000, 3000).run();
        if (sp === '移动' && province === '广东') {
          none.click(`div.tWGpNA2Y:contains(广东移动)`).delay().run();
        }
        none
          // 点击确定
          .click('._2YJgLay-').delay(500, 3000)
          .run({ errResume: true });
      }
    }]
  }
}



// 水莉机器 
const {
  // 管理后台接口
  CMS_API_HOST,
  // 管理后台地址
  ADMIN_HOST,
  // 系统用户名
  SYS_USERNAME = 'robot',
  // 系统密码
  SYS_PASSWORD = '9090990',
  // 系统万能验证码
  SYS_CAPTCHA = 'l4xz5',
  // 支付密码
  ALIPAY_PASSWORD = '369258',

  // 拼多多账号
  PHONE = '18820460676',
  // 开始时间,启动之日起往前10天
  START_TIME = +new Date(new Date() - 10 * 24 * 60 * 60 * 1000),
  GOODS_MAP = [{

    GOODS_TITLE: '保鲜膜套袋',
    GOODS_LINK: 'http://yangkeduo.com/mall_page.html?mall_id=279885972&msn=nj2vyughkhbrkehzlp3welgoaa_axbuy&mall_info=%7B%22mall_name%22%3A%22%E5%A5%87%E8%BF%B9%E5%AE%B6%E5%B1%85%E7%99%BE%E8%B4%A7%22%7D&goods_id=248152640825&refer_page_name=goods_detail&refer_page_id=10014_1625744727789_vkb7d1ge8a&refer_page_sn=10014',
    ITEM_IDS: [
      '1407262845359001602',
      '1405369875522772994',
      '1387677031977353218',
      '1387677933794656258',
    ],
    // 价格区间检查
    PAY_RANGE: [0, 9],
    type: GOODS_TYPES.实物商品,
    // 洪东通信专营店
    async operate() {
      await none.delay(100, 200)
        // 去拼单
        .click('#loacl-group-container .Ay9Qe_QL:last-child').delay(1000, 3000)
        // 参与拼单
        .click('button:contains(参与拼单)').delay(1000, 3000)
        // 选择sku运营商
        .click(`div.sku-spec-value:contains(加厚升级款【2只】不划算)`).delay(1000, 3000).run();
      none
        // 点击确定
        .click('._2YJgLay-').delay(500, 3000)
        .run({ errResume: true });
    }
  }]

} = envs[env];

const none = new None({
  name: '自动下单',
});


let goodsConfig = null;

//测试代码
if (0) {
  ~async function () {
    console.log(`---测试开始!`)
    // ------测试代码-------
    // const doneOrderIdList = await none.bgStore('autoOrder-done') || [];
    // console.log("doneOrderIdList---", doneOrderIdList);
    none.on('test', (data) => {
      console.log('========================')
      console.log(data);
    })
    none.emit('test', { a: 5 })
    // 卡片通知测试
    // test_card();
    console.log(`---测试结束!`)
  }()
}


~async function () {
  console.log('自动化脚本开始运行，请确保auto.vbs已经运行');
  const hosts = {
    // 拼多多自动化脚本
    'txiat.cn': txiat,
  }
  // console.log(hosts);
  if (hosts[location.hostname]) {
    console.log(`当前域名为${location.hostname}`)
    await None.sleep(2000);
    hosts[location.hostname](None, none);
  }
  return false;
}();


// 拼多多页面自动化 ------拼多多-------------------------------------
async function auto_pinduoduo(None, none) {
  
}

// 运行任务
function openPage(url) {
  const a = document.createElement('a');
  a.target = '_blank';
  a.href = url
  a.click();
}
